package com.example.springMVC.rmi;

import com.sun.jndi.rmi.registry.ReferenceWrapper;
import lombok.extern.log4j.Log4j2;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.naming.NamingException;
import javax.naming.Reference;
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

@Log4j2
public class RMIService {

    public static void main(String[] args) {
        try {
            LocateRegistry.createRegistry(1099);
            Registry registry = LocateRegistry.getRegistry();
            System.out.println("创建RMI");
            Reference reference = new Reference("com.example.springMVC.rmi.EvilObj", "com.example.springMVC.rmi.EvilObj", null);
            ReferenceWrapper referenceWrapper = new ReferenceWrapper(reference);
            registry.bind("evil",referenceWrapper);
        } catch (RemoteException | NamingException | AlreadyBoundException e) {
            e.printStackTrace();
        }
    }














//    @Test
//    public void testHello() throws Exception {
//        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml","springa.xml");
//        Student s1 = context.getBean("student", Student.class);
//        Student s2 = context.getBean("student", Student.class);
//        student.getName();

//        Integer userCount = userMapper.getUserCount();
//        System.out.println(userCount);





        // 1.通过ClassPathXmlApplicationContext实例化Spring的上下文
        //        Hello hello = (Hello)context.getBean("hello");

//        JavaConnection conn = (JavaConnection)context.getBean("conn");
//        for (Object o : conn.getList()) {
//            System.out.println(o);
//        }
//        System.out.println(conn.getSet());
//        System.out.println(conn.getAddressProp());

//        Greeting zhangGaSay = context.getBean("zhangGaSay", Greeting.class);
//        Greeting rodSay = context.getBean("rodSay", Greeting.class);
//        zhangGaSay.sayGreeting();
//        rodSay.sayGreeting();

//        DaYinJi dayinji1 = context.getBean("dayinji2", DaYinJi.class);
//        dayinji1.print();


//        Student student = context.getBean("stu",Student.class);
//        student.getName();
//        student.ex();

//          before   around1     afterthrow   after
//          before   aronud1     around2    after-return   after
//        User user = context.getBean("user",User.class);

//        UserService userService = context.getBean("userServiceImpl", UserService.class);
    }

//}
